<?php
/**
 * Objeto Relacion de Modelos
 * @author Crash (Carlos E. Pena A.)
 * Elaborado 28 de Septiembre de 2013
 *
 */

class MOrm Extends CI_Model {

	/**
	 * Auxiliar de Consulta
	 */
	private $sCon = '';

	/**
	 * Tabla Principal de Conexion
	 */
	private $tabla = '';

	/**
	 * Campos de Seleccion
	 */
	private $set = '*';

	/**
	 * Selecion Simple del Codigo
	 */
	private $select = '';

	/**
	 * Unir Varias Tablas
	 */
	private $join = '';

	/**
	 * Donde Elementos
	 */
	private $where = '';

	/**
	 * Grupo de Elementos
	 */
	private $group = '';

	private $arr = array();

	private $param = 0;

	/**
	 * Crear Base de Datos
	 *
	 */

	function _CreateBD() {
		$sCon = 'CREATE DATABASE ';
	}

	/**
	 * Generar Conjunto de Tablas
	 * @param Modelo de Datos
	 * @param arr Datos
	 */
	function _GenerarTB($sMod = null) {
		
		if($this->param == 0)	{$this -> arr[] = $this -> _CreateTB($sMod->tbl, $sMod->cmp, $sMod->dtd);}		
		foreach ($sMod -> cmp['join'] as $Clv => $Val) {
			$mdl = "X_" . $this -> param++;
			$this -> load -> model($Clv, $mdl);
			$this -> arr[] = $this -> _CreateTB($this -> $mdl->tbl, $this -> $mdl->cmp, $this -> $mdl->dtd);
			if (isset($this -> $mdl -> cmp['join'])) {					
					$this -> _GenerarTB($this -> $mdl);
			}		
			unset($mdl);
		}		
	}

	/**
	 * Crear Tabla de Datos InnoDB y MyIsam
	 * @param Nombre de la Tabla (Innodb, MyIsam)
	 * @param Arreglo de Campo Clave y Valor
	 * @param Descripcion de la tabla
	 */
	private function _CreateTB($tabla = null, $cmp = array(), $dtd = '') {
		$arr = array();
		// Formato del Campo
		$arrA = array();
		// En los Casos PRIMARY, INDEX, FORINGE KEY, FULLTEXT
		$sClaves = '';
		// Creando lineas de Tablas
		$sCadena = '';
		//Constuyendo lineas del Create
		$sCreate = 'CREATE TABLE IF NOT EXISTS ' . $tabla . ' (';
		$sComentario = '';
		$sTipo = '';
		$i = 0;
		$iCmp = count($cmp);
		
		
		foreach ($cmp as $sClv => $sVal) {
			//Palabras reservadas para el Query y Otros
			$i++;
			if ($sClv != 'set' && $sClv != 'join' && $sClv != 'where' && $sClv != 'arit' && $sClv != 'group' && $sClv != 'left') {
				if (isset($sVal['comentario'])) {
					$sComentario = 'COMMENT \'' . $sVal['comentario'] . '\'';
				}
				if (isset($sVal['index'])) {
					
					if($sVal['index'] == 'KEY'){
						$sClaves .= ', KEY (' . $sClv . ')';
					}else{
						$sClaves .= ' PRIMARY KEY (' . $sClv . ')';
					}
				}
				$sTipo = 'VARCHAR (32) NOT NULL';
				if (isset($sVal['tipo'])) {
					$sTipo = $sVal['tipo'];
				}

				$sCreate .= ' ' . $sClv . ' ' . $sTipo . ' ' . $sComentario;
				if ($i < $iCmp) {
					$sCreate .= ',';
				}
				$sComentario = '';

			}

		}
		$sCreate .= $sClaves . ') ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT=\'\';';
		return $sCreate;
	}

	/**
	 * Crear Select
	 * @param Arreglos de Where
	 *
	 * @return string (SQL)
	 */
	private function _getSelect() {

		$sCon = 'SELECT ' . $this -> set . ' FROM ' . $this -> tabla . ' ';
		return $sCon;
	}

	/**
	 * Crear Set
	 * @param Arreglos de Campos
	 *
	 * @return string (SQL)
	 */
	private function _getSet($set = array()) {

	}

	/**
	 * Crear Join
	 * @param Arreglos de las Uniones
	 *
	 * @return string (SQL)
	 */
	private function _getJoin($tabla, $join = array()) {
		$sCon = '';
		$sQ = '';
		foreach ($join as $Clv => $Val) {
			$mdl = "T_" . $this -> param++;
			$this -> load -> model($Clv, $mdl);
			if (isset($this -> $mdl -> cmp['join'])) {
				if (count($this -> $mdl -> cmp['join']) > 0) {
					$sQ .= $this -> _getJoin($this -> $mdl -> tbl, $this -> $mdl -> cmp['join']);
				}
			}
			$sCon .= ' JOIN ' . $this -> $mdl -> tbl;
			foreach ($Val as $sC => $sV) {
				$sCon .= ' ON ' . $this -> $mdl -> tbl . '.' . $sC . '=' . $tabla . '.' . $sV;
			}
			unset($mdl);
		}
		return $sCon . $sQ;
	}

	/**
	 * Crear Select
	 * @param Arreglos de Where
	 *
	 * @return string (SQL)
	 */
	private function _getWhere($where = array()) {

	}

	/**
	 * Crear GROUP BY
	 * @param Arreglos de Group
	 *
	 * @return string (SQL)
	 */
	private function _getGroup($group = array()) {

	}

	/**
	 * Generar Codigo SQL
	 * @param Tabla Principal
	 * @param Modelo de Datos
	 *
	 * @return string
	 */
	function getSQL($sModelo = null) {

		$this -> load -> model($sModelo, 'MObjeto');
		$this -> tabla = $this -> MObjeto -> tbl;
		
		$this -> _GenerarTB($this -> MObjeto);
		
		foreach ($this->arr as $sC => $sV) {
			if ($sV != '') {
				$this -> db -> query($sV);
			}
		} 
		
		$this -> sCon .= $this -> _getSet();
		$this -> sCon .= $this -> _getSelect();
		$this -> sCon .= $this -> _getJoin($this -> tabla, $this -> MObjeto -> cmp['join']);
		$this -> sCon .= $this -> _getWhere();
		echo $this -> sCon;
		return $this -> sCon;
	}

}
?>